package tech.abing.spider.task.pageprocessor.amac;

import java.sql.Timestamp;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.constants.DictEnum;
import tech.abing.spider.task.model.amac.IntegrityPublicityEntity;
import tech.abing.spider.util.DateUtils;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

public class FalseFillProcessor implements PageProcessor{

	private static Logger logger = Logger.getLogger(FalseFillProcessor.class);
	
	private static int record = 0;
	
	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		Document doc = Jsoup.parse(page.getContent());
		PageRequest request = page.getRequest();
		Element table = doc.select("table.table.table-hover > tbody").first();
		Elements trs = table.select("tr");
		for (Element tr : trs) {
			record++;
			logger.info("正在解析第"+record+"条数据:");
			IntegrityPublicityEntity ipe = new IntegrityPublicityEntity();
			Elements tds = tr.select("td");
			ipe.setCreateTime(new Timestamp(System.currentTimeMillis()));
			ipe.setManagerName(tds.get(1).text());
			ipe.setRegisterNo(tds.get(2).text());
			if(StringUtils.isNotEmpty(tds.get(3).text())){
				ipe.setRegisterDate(DateUtils.parseDate(tds.get(3).text()));
			}
			if(StringUtils.isNotEmpty(tds.get(4).text())){
				ipe.setFoundDate(DateUtils.parseDate(tds.get(4).text()));
			}
			ipe.setRegisterAddress(tds.get(5).text());
			ipe.setSituation(tds.get(6).text());
			ipe.setIsChange(tds.get(7).text());
			ipe.setPublicityType(DictEnum.TASK_TYPE_FALSE_FILL.getName());
			ipe.setUrl(request.getUrl());
			ipe.setId(UuIdPublic.timeStrongID(5));
			StringUtils.removeSpace(ipe);
			objectContainer.add(ipe);
		}
		
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}


}
